<template>
  <div class="mobile-container">
    <div class="app-content">
      <!-- 顶部栏 -->
      <div class="header">
        <div class="header-top">
          <div class="back-icon" @click="handleBack">
            <Icon icon="mdi:arrow-left"/>
          </div>
          <div class="page-title">余额</div>
          <div class="placeholder"></div>
        </div>
      </div>

      <!-- 余额兑换内容 -->
      <div class="balance-content">
        <!-- 余额显示区域 -->
        <div class="balance-display">
          <div class="balance-title">可兑换余额</div>
          <div class="balance-amount">
            <div class="amount-value" v-if="showAmount">{{ exchangeAmount }}</div>
            <div class="amount-value" v-else>****</div>
          </div>
          <div class="balance-tip"  v-if="!showAmount" @click="showAmount = true">点击查看余额</div>
          <div class="balance-tip" v-else @click="showAmount = false">点击隐藏余额</div>
        </div>

        <!-- 兑换记录区域 -->
        <div class="exchange-records">
          <div class="records-header">
            <div class="records-title">兑换记录</div>
            <div class="records-filter">
              <button class="filter-btn" :class="{ active: filterType === 'all' }" @click="filterType = 'all'">
                全部
              </button>
              <button class="filter-btn" :class="{ active: filterType === 'completed' }" @click="filterType = 'completed'">
                已完成
              </button>
              <button class="filter-btn" :class="{ active: filterType === 'pending' }" @click="filterType = 'pending'">
                处理中
              </button>
            </div>
          </div>

          <!-- 空状态 -->
          <div class="empty-state" v-if="filteredRecords.length === 0">
            <div class="empty-icon">
              <Icon icon="mdi:file-document-outline" />
            </div>
            <div class="empty-text">数据为空</div>
            <div class="empty-tip">暂无兑换记录</div>
          </div>

          <!-- 兑换记录列表 -->
          <div class="records-list" v-else>
            <div class="record-item" v-for="record in filteredRecords" :key="record.id">
              <div class="record-info">
                <div class="record-type">{{ record.type }}</div>
                <div class="record-date">{{ record.date }}</div>
              </div>
              <div class="record-amount">
                <div class="amount" :class="{ 'positive': record.status === 'completed', 'pending': record.status === 'pending' }">
                  {{ record.amount }} WP
                </div>
                <div class="record-status" :class="record.status">
                  {{ getStatusText(record.status) }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Icon } from '@iconify/vue2'

export default {
  name: 'BalanceExchangePage',
  components: {
    Icon
  },
  data() {
    return {
      showAmount: false,
      showExchangeDialog: false,
      isCounting: false,
      countdown: 60,
      countdownText: '获取验证码',
      filterType: 'all',
      exchangeAmount: 2048,
      verificationCode: '',
      selectedPayment: 'balance',
      exchangeRecords: [
        {
          id: 1,
          type: '余额兑换',
          date: '2023-11-01 14:30',
          amount: 100,
          status: 'completed'
        },
        {
          id: 2,
          type: '余额兑换',
          date: '2023-10-28 09:15',
          amount: 50,
          status: 'pending'
        }
      ]
    }
  },
  computed: {
    filteredRecords() {
      if (this.filterType === 'all') {
        return this.exchangeRecords
      }
      return this.exchangeRecords.filter(record => record.status === this.filterType)
    },
    isExchangeFormValid() {
      return this.exchangeAmount &&
        this.exchangeAmount > 0 &&
        this.verificationCode.length === 6
    }
  },
  methods: {
    handleBack() {
      this.$router.go(-1)
    },

    getStatusText(status) {
      const statusMap = {
        completed: '已完成',
        pending: '处理中',
        failed: '失败'
      }
      return statusMap[status] || '未知'
    },

    async getVerificationCode() {
      if (this.isCounting) return

      this.isCounting = true
      this.countdown = 60

      const timer = setInterval(() => {
        this.countdown--
        this.countdownText = `${this.countdown}s后重新获取`

        if (this.countdown <= 0) {
          clearInterval(timer)
          this.isCounting = false
          this.countdownText = '获取验证码'
        }
      }, 1000)

      // TODO: 调用获取验证码接口
      console.log('发送验证码到手机')
    },

    submitExchange() {
      if (!this.isExchangeFormValid) return

      // TODO: 调用兑换接口
      console.log('提交兑换申请:', {
        amount: this.exchangeAmount,
        payment: this.selectedPayment,
        code: this.verificationCode
      })

      // 模拟兑换成功
      this.showExchangeDialog = false
      this.exchangeAmount = ''
      this.verificationCode = ''

      // 显示成功提示
      alert('兑换申请已提交，请等待处理')
    }
  }
}
</script>

<style scoped>
.mobile-container {
  width: 100%;
  min-height: 100vh;
  background: #f5f5f5;
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  padding: 20px;
}

.app-content {
  width: 375px;
  min-height: 667px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  position: relative;
  display: flex;
  flex-direction: column;
}

/* 顶部栏样式 */
.header {
  padding: 15px 20px;
  background: white;
  border-bottom: 1px solid #f0f0f0;
}

.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.back-icon {
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #333;
}

.back-icon:hover {
  background: #f5f5f5;
}

.page-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.placeholder {
  width: 36px;
}

/* 余额内容样式 */
.balance-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 20px;
}

.balance-display {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 16px;
  padding: 30px 20px;
  text-align: center;
  color: white;
  margin-bottom: 24px;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.balance-title {
  font-size: 16px;
  margin-bottom: 10px;
  opacity: 0.9;
}

.balance-amount {
  margin-bottom: 12px;
}

.amount-value {
  font-size: 36px;
  font-weight: 700;
}

.balance-tip {
  font-size: 14px;
  opacity: 0.7;
}


.balance-tip:hover {
  cursor: pointer;
}

/* 兑换记录区域 */
.exchange-records {
  flex: 1;
  background: white;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.records-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.records-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.records-filter {
  display: flex;
  gap: 8px;
}

.filter-btn {
  padding: 6px 12px;
  border: 1px solid #e0e0e0;
  border-radius: 16px;
  background: white;
  font-size: 12px;
  color: #666;
  cursor: pointer;
  transition: all 0.3s ease;
}

.filter-btn.active {
  background: #667eea;
  color: white;
  border-color: #667eea;
}

/* 空状态样式 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #999;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-text {
  font-size: 16px;
  margin-bottom: 8px;
  color: #666;
}

.empty-tip {
  font-size: 14px;
  opacity: 0.7;
}

/* 记录列表样式 */
.records-list {
  max-height: 300px;
  overflow-y: auto;
}

.record-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0;
  border-bottom: 1px solid #f0f0f0;
}

.record-item:last-child {
  border-bottom: none;
}

.record-info {
  flex: 1;
}

.record-type {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin-bottom: 4px;
}

.record-date {
  font-size: 12px;
  color: #999;
}

.record-amount {
  text-align: right;
}

.amount {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 4px;
}

.amount.positive {
  color: #07C160;
}

.amount.pending {
  color: #FAAD14;
}

.record-status {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 10px;
  display: inline-block;
}

.record-status.completed {
  background: #f6ffed;
  color: #52c41a;
  border: 1px solid #b7eb8f;
}

.record-status.pending {
  background: #fffbe6;
  color: #faad14;
  border: 1px solid #ffe58f;
}

.record-status.failed {
  background: #fff2f0;
  color: #ff4d4f;
  border: 1px solid #ffccc7;
}

/* 响应式设计 */
@media (max-width: 375px) {
  .mobile-container {
    padding: 0;
  }

  .app-content {
    width: 100%;
    min-height: 100vh;
    border-radius: 0;
  }

  .exchange-dialog {
    width: 280px;
  }
}
</style>
